package com.dragonfly.shutter.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.services.InformAgainstPictureService;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 举报图片控制类
 * @author 黄挺
 */
@Controller
@RequestMapping("/informAgainstPicture.html")
@SessionAttributes("validateCode")
public class InformAgainstPictureController {
	@Autowired
	InformAgainstPictureService informAgainstPictureService;
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView onLoad(HttpSession session, HttpServletRequest request,
			@RequestParam(value="pictureID", required=false) String pictureID,
			@RequestParam(value="reportPictureValidateCode", required=false) String reportPictureValidateCode,
			@ModelAttribute("validateCode") String validateCode
			){
		Message message = null;
		Integer userID = (Integer) session.getAttribute("userID");
		
		//空的参数
		if(pictureID == null ){
			message = new Message("空的参数", "操作失败", "picture.html?pictureID="+pictureID);
			return new ModelAndView("message", "message", message);
		}
		
		//非法的参数
		if(!KeywordFilter.isNumeric(pictureID)){
			message = new Message("非法的参数", "操作失败", "picture.html?pictureID="+pictureID);
			return new ModelAndView("message", "message", message);
		}
		
		//验证码错误
		if(!validateCode.equals(reportPictureValidateCode.toUpperCase())){
			message = new Message("验证码错误，请重新填写", "操作失败", "picture.html?pictureID="+pictureID);
			return new ModelAndView("message", "message", message);
		}
		
		informAgainstPictureService.doService(pictureID, userID, request.getRequestURL().toString());
		return new ModelAndView("redirect:/picture.html?pictureID="+pictureID);
	}
}
